<template>
  <div class="user-info">
    <!-- 添加加载状态显示 -->
    <div v-if="loading" class="loading">
      加载中，请稍候...
    </div>
    <el-card class="box-card" v-else-if="userInfo">
      <template v-slot:header>
      <div class="clearfix">
          <span>个人信息</span>
      </div>
      </template>
      <el-form label-position="left" label-width="100px">
        <el-form-item label="用户名">
          <span>{{ userInfo.userName }}</span>
        </el-form-item>
        <el-form-item label="性别">
          <span>{{ userInfo.gender }}</span>
        </el-form-item>
        <el-form-item label="出生日期">
          <span>{{ userInfo.birthDate }}</span>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 如果userInfo为空且不是加载状态，显示无数据提示 -->
    <div v-else class="no-data">
      暂无用户信息
    </div>
    <router-link :to="{ name: 'location' }" class="btn">
      <i class="el-icon-plus"></i>返回用户界面
    </router-link>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      userInfo: null,
      loading: true  // 初始设置为正在加载数据
    };
  },
  created() {
    this.fetchUserInfo();
  },
  methods: {
    fetchUserInfo() {
      const token = localStorage.getItem('userToken');
      console.log('Token used:', token); // 输出使用的Token，确认其正确性
      axios.get('/api/user/info', {
        headers: {
          'Authorization': token,
        }
      })
      .then(response => {
        console.log('API Response:', response.data); // 输出API响应数据
        this.userInfo = response.data.data;//这里真的，总是出错
        this.loading = false;
      })
      .catch(error => {
        console.error('Error fetching user info:', error); // 输出错误信息
        this.loading = false;
        this.$message.error('获取用户信息失败');
      });
    }
  }
};
</script>


  